/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fbv.tcc.xls;

import fbv.tcc.classes.PontoRadiacao;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class WriteExcel {

    private WritableCellFormat arialBoldUnderline;
    private WritableCellFormat arial;
    private String inputFile;

    public void setOutputFile(String inputFile) {
        this.inputFile = inputFile;
    }

    public void write(ArrayList<PontoRadiacao> ptPlanilha, ArrayList<PontoRadiacao> ptModeloFisico) throws IOException, WriteException {
        File file = new File(inputFile);
        WorkbookSettings wbSettings = new WorkbookSettings();


        WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
        workbook.createSheet("Ocorrencias", 0);
        WritableSheet excelSheet = workbook.getSheet(0);
        createLabel(excelSheet);
        createContent(excelSheet, ptPlanilha, ptModeloFisico);

        workbook.write();
        workbook.close();
    }

    private void createLabel(WritableSheet sheet)
            throws WriteException {

        // Lets create a times font
        WritableFont times10pt = new WritableFont(WritableFont.ARIAL, 10);

        // Define the cell format
        arial = new WritableCellFormat(times10pt);

        // Lets automatically wrap the cells
        arial.setWrap(false);

        // Create create a bold font with unterlines
        WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false,
                UnderlineStyle.NO_UNDERLINE);
        arialBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
        // Lets automatically wrap the cells
        arialBoldUnderline.setWrap(true);

        CellView cv = new CellView();
        cv.setFormat(arial);
        cv.setFormat(arialBoldUnderline);
        cv.setAutosize(true);

        // Write a few headers
        addCaption(sheet, 0, 0, "DIA JULIANO");
        addCaption(sheet, 1, 0, "MINUTO DIA");
        addCaption(sheet, 2, 0, "OCORRENCIA");

    }

    private void createContent(WritableSheet sheet, ArrayList<PontoRadiacao> ptPlanilha, ArrayList<PontoRadiacao> ptModeloFisico) throws WriteException,
            RowsExceededException {

        // Write a few number      
        int flag;

        
        for (int i = 1; i < ptModeloFisico.size(); i++) {

            double radiacaoPlanilha = ptPlanilha.get(i).getRadiacao();
            double radiacaoModeloFisico = ptModeloFisico.get(i).getRadiacao();

            

            if (radiacaoPlanilha > radiacaoModeloFisico) {
                flag = 1;
            } else {
                flag = 0;
            }

            addNumber(sheet, 0, i, ptModeloFisico.get(i).getTimeStamp().getDayOfYear());
            addNumber(sheet, 1, i, ptModeloFisico.get(i).getTimeStamp().getMinuteOfDay());
            addNumber(sheet, 2, i, flag);

        }


    }

    private void addCaption(WritableSheet sheet, int column, int row, String s)
            throws RowsExceededException, WriteException {
        Label label;
        label = new Label(column, row, s, arialBoldUnderline);
        sheet.addCell(label);
    }

    private void addNumber(WritableSheet sheet, int column, int row,
            Integer integer) throws WriteException, RowsExceededException {
        Number number;
        number = new Number(column, row, integer, arial);
        sheet.addCell(number);
    }

    private void addDouble(WritableSheet sheet, int column, int row,
            Double _double) throws WriteException, RowsExceededException {
        Number number;
        number = new Number(column, row, _double, arial);
        sheet.addCell(number);
    }
}